package main

import (
	"fmt"
	"github.com/julienschmidt/httprouter"
	"log"
	"net/http"
)

//原始的http服务
//func main() {
//	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//		fmt.Fprintf(w, "hello world")
//	})
//	http.HandleFunc("/time", func(w http.ResponseWriter, r *http.Request) {
//		t := time.Now()
//		timeStr := fmt.Sprintf("{\"time\":\"%s\"", t)
//		w.Write([]byte(timeStr))
//
//	})
//
//	http.ListenAndServe(":8088", nil)
//
//}

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcom!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	fmt.Fprint(w, "Hello ", ps.ByName("name"))
}

//使用http-router
func main() {
	router := httprouter.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)
	log.Fatal(http.ListenAndServe(":8088", router))
}
